const express = require('express');
const router = express.Router();
//插入数据
router.post('/addmovie', (req, res, next) => {
  const body = req.body
  const mo = {
    doctor: body.doctor,
    title: body.title,
    language: body.language,
    country: body.country,
    year: Number(body.year),
    poster: body.poster,
    summary: body.summary,
    flash: body.flash,
  }
  const movies = new Movie(mo)
  let response = {
    data: '',
    errmsg: '',
    error: 99999
  }
  let flag = true
  if (!mo.year) {
    flag = false
    response.errmsg = '请输入正确的年份'
  }
  if (!flag) {
    return res.json(response)
  } else {
    movies.save().then(() => {
      const response = createResult('添加成功')
      res.json(response)
    }).catch(err => {
      throw err
    })
  }

})
//删除数据
router.post('/deletemovie', (req, res, next) => {
  const id = req.body.id
  console.log(id)
  let response = createResult('id参数错误')
  if(!id) {
    return res.json(response);
  }
  
  Movie.remove({
    _id: id
  }).then(() => {
    console.log('删除成功')
    response = createResult('删除成功')
    res.json(response);
  }).catch(err => {
    throw err
  })
});
//修改数据
router.post('/updatemovie', (req, res, next) => {
  const body = req.body
  const obj = {
    doctor: body.doctor,
    title: body.title,
    language: body.language,
    country: body.country,
    year: Number(body.year),
    poster: body.poster,
    summary: body.summary,
    flash: body.flash,
  }
  Movie.update({_id:body.id},obj).then(() => {
    const response = createResult('更新成功')
    res.json(response)
  }).catch(err => {
    throw err
  })
})
module.exports = router